#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

import sys
import os
import requests
import time


def getTime():
    return time.strftime('%Y%m%d %H:%M:%S', time.localtime())


def validate(file):
    check = 1
    if not os.path.exists(file):
        check = 0
    else:
        infile = open(file, "rb")
        l3 = ""

        for line in infile:
            line = line.rstrip()
            if not line == "":
                l3 = line
        infile.close()
        if l3.find("%%EOF") == -1:
            check = 5
            infile2 = open(file, "rb")
            for line2 in infile2:
                line2 = line2.rstrip()
                if line2.find("retracted") != -1:
                    check = 2
                    break
                if line2.find("WWW Error Blocked Diagnostic") != -1:
                    check = 3
                    break
                if line2.find("Page not available") != -1:
                    check = 4
                    break
            infile2.close()
            os.remove(file)
    return check


def download(path, id):
    try:
        url = 'http://www.ncbi.nlm.nih.gov/pmc/articles/PMC' + id + '/pdf'
        r = requests.get(url, timeout=180)
        filepath = path + "/PMC" + id + ".pdf"
        with open(filepath, "wb") as code:
            code.write(r.content)
        print getTime() + " PMC" + id + ".pdf has been downloaded."
        code.close()
    except:
        print getTime() + " PMC" + id + ".pdf download exception."


if __name__ == "__main__":
    infile = open(sys.argv[1], 'r')
    number = sys.argv[1].split("_")[3][:-5]
    path = "pmc_pdf_" + number
    if not os.path.exists(path):
        os.mkdir(path)
    path = os.path.abspath(path)

    for line in infile:
        line = line.rstrip()
        file = path + "/PMC" + line + ".pdf"
        filename = "PMC" + line + ".pdf"
        retrytimes = 0
        while retrytimes < 3:
            time.sleep(3)
            val = validate(file)
            if val == 1:
                break
            elif val == 5:
                print getTime() + " " + filename + " is invalid."
            elif val == 2:
                print getTime() + " " + filename + " is retracted."
                break
            elif val == 4:
                print getTime() + " " + filename + " is not available."
                break
            elif val == 3:
                print getTime() + " this IP is blocked."
                blockfile = open("BLOCKED.IP", "w")
                blockfile.write(getTime() + " this IP is blocked.")
                blockfile.flush()
                blockfile.close()
                sys.exit()
            download(path, line)
            retrytimes += 1
    infile.close()
